package com.hz.common.freemarker;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Interface {
	private final static PropertiesUtil propertiesUtil = new PropertiesUtil();

	Configuration freeMarkerCfg = new Configuration();
	Template template = null;

	public Interface() {
		freeMarkerCfg.setClassForTemplateLoading(getClass(), "");
		freeMarkerCfg.setObjectWrapper(new DefaultObjectWrapper());

		freeMarkerCfg.setClassForTemplateLoading(this.getClass(), "/com/hz/common/freemarker/template");
		try {
			template = freeMarkerCfg.getTemplate("Interface.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void generateRequest(TemplateValue tv) {
		String path = propertiesUtil.getInterfacePath();
		String reqFileName = path + tv.getEntityName() + "Interface.java";
		try {
			Map<String, Object> parameters = new HashMap<String, Object>();

			parameters.put("p", tv);

			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(reqFileName), "UTF-8");
			template.process(parameters, writer);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();// TODO
		} catch (TemplateException e) {
			e.printStackTrace();// TODO
		}

	}

}
